﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StateGrid;
using StateGrid.KJCG;
using StateGrid.User;
using StateGrid.GlobalVariables;
using iTextSharp.text.pdf;
using StateGrid.OperationLog;

public partial class AddPM : System.Web.UI.Page
{
    /// <summary>
    /// 科技成果操作对象
    /// </summary>
    private KJCG_DAO kjcgDAO = new KJCG_DAO();
    /// <summary>
    /// 日志操作对象
    /// </summary>
    private OperationLog_DAO operationLogDAO = new OperationLog_DAO();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!UserManageDAO.IsLogin((User)Session["login"]))
        {
            GlobalMethod.RedirectLogin(Response);
            return;
        }

        if (!this.IsPostBack)
        {
            this.BindData();
        }

    }

    /// <summary>
    /// 绑定初始化数据
    /// </summary>
    private void BindData()
    {

        String message = null;
        String cgdm = HttpUtility.UrlDecode(Request["cgdm"]);
        if (null == cgdm || "".Equals(cgdm))
        {
            message = "错误：参数错误";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }

        //指定成果代码
        KJCG_PM kjcgPM = this.kjcgDAO.GetKJCG_PMByCgdm(cgdm);

        if (null == kjcgPM)
        {
            message = "错误：没有找到该科技成果";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }

        //获取对应的分类信息
        KJCG_FL kjcgFL = this.kjcgDAO.GetKJCG_FLByFlbm(kjcgPM.flbm);

        User user = (User)Session["login"];
        //登陆超时则退到登录页
        if (null == user)
        {
            Response.Write(" <script> top.location = '" + GlobalResource.LOGIN_PAGE + "' </script> ");
            return;
        }

        //登录人
        this.dlrTextBox.Text = user.name;

        //设置返回按钮链接
        this.backHyperLink.NavigateUrl = GlobalKJCG.LISTPM_PAGE + "?catalog=" + kjcgPM.flbm;

        //分类编码
        this.flbmTextBox.Text = kjcgFL.flbm;
        //分类名称
        this.flmcTextBox.Text = kjcgFL.flmc;
        //专业代码
        this.zydmTextBox.Text= kjcgPM.zydm;
        //成果代码
        this.cgdmTextBox.Text = cgdm;
        //成果年份
        this.cbnfTextBox.Text = cgdm.Split('-')[0];

        //中文名称
        this.zwmcTextBox.Text = kjcgPM.zwmc;
        //学科代码
        this.xkdmTextBox.Text = kjcgPM.xkdm;
        //中文主题词
        this.zwztcTextBox.Text = kjcgPM.zwztc;
        this.pjfsTextBox.Text = kjcgPM.pjfs;//评价方式
        this.pjdwTextBox.Text = kjcgPM.pjdw;//评价单位
        this.zwwzTextBox.Text = kjcgPM.zwwz;//中文文摘
        //评价日期
        if (null != kjcgPM.pjrq)
        {
            DateTime dt = (DateTime)kjcgPM.pjrq;
            this.pjrqTextBox.Text = dt.ToString("yyyy-MM-dd");
        }
        //评价意见
        this.pjyjTextBox.Text = kjcgPM.pjyj;
        if (null != kjcgPM.qsrq)
        {
            DateTime dt = (DateTime)kjcgPM.qsrq;
            this.qsrqTextBox.Text = dt.ToString("yyyy-MM-dd");//起始日期
        }
        if (null != kjcgPM.zzrq)
        {
            DateTime dt = (DateTime)kjcgPM.zzrq;
            this.zzrqTextBox.Text = dt.ToString("yyyy-MM-dd");//终止日期
        }
        this.jxmcTextBox.Text = kjcgPM.jxmc;//奖项意见
        this.jldwTextBox.Text = kjcgPM.jldw;//奖励单位
        this.jlndTextBox.Text = kjcgPM.jlnd;//奖励年度
        this.sjdjTextBox.Text = kjcgPM.sjdj;//授奖等级
        this.cqxsTextBox.Text = kjcgPM.cqxs;//产权形式
        this.cqmcTextBox.Text = kjcgPM.cqmc;//产权名称
        this.cqbsTextBox.Text = kjcgPM.cqbs;//产权标识
        this.dydwTextBox.Text = kjcgPM.dydw;//第一单位
        this.dwsxTextBox.Text = kjcgPM.dwsx; //单位属性
        this.lsgxTextBox.Text = kjcgPM.lsgx; //隶属关系

        this.txdzTextBox.Text = kjcgPM.txdz;//通讯地址
        this.lxdhTextBox.Text = kjcgPM.lxdh;//联系电话
        this.czdhTextBox.Text = kjcgPM.czdh;//传真电话
        this.lxrTextBox.Text = kjcgPM.lxr;//联系人
        this.dzxxTextBox.Text = kjcgPM.dzxx;//电子邮箱
        this.qtdwTextBox.Text = kjcgPM.qtdw;//其他单位
        this.dywcrTextBox.Text = kjcgPM.dywcr;//第一完成人
        this.zwzcTextBox.Text = kjcgPM.zwzc;//职务职称
        this.szdwTextBox.Text = kjcgPM.szdw;//所在单位
        this.dhTextBox.Text = kjcgPM.dh;//电话
        this.qtwcrTextBox.Text = kjcgPM.qtwcr;//其他完成人
        this.tjdwyjTextBox.Text = kjcgPM.tjdwyj;//推荐单位意见

        this.SetPreAndNextPM();
    }

    /// <summary>
    /// 获取用户填写的科技成果篇名信息
    /// </summary>
    /// <returns></returns>
    private KJCG_PM GetKJCGPM()
    {
        KJCG_PM kjcgPM = new KJCG_PM();
        //成果代码
        kjcgPM.cgdm = this.cgdmTextBox.Text;
        //分类编码
        kjcgPM.flbm = this.flbmTextBox.Text;
        //上报编码
        kjcgPM.sbbm = this.sbdmTextBox.Text;
        //专业代码
        kjcgPM.zydm = this.zydmTextBox.Text.Trim();
        //中文名称
        kjcgPM.zwmc = this.zwmcTextBox.Text.Trim();
        //成果类别
        kjcgPM.cglb = this.cglbDropDownList.SelectedValue;
        //起始日期
        if (!"".Equals(this.qsrqTextBox.Text))
            kjcgPM.qsrq = DateTime.Parse(this.qsrqTextBox.Text);
        //终止日期
        if(!"".Equals(this.zzrqTextBox.Text))
            kjcgPM.zzrq = DateTime.Parse(this.zzrqTextBox.Text);
        //学科代码
        kjcgPM.xkdm = this.xkdmTextBox.Text;
        //国图分类
        kjcgPM.gtfl = this.gtflTextBox.Text;
        //中文主题词
        kjcgPM.zwztc = this.zwztcTextBox.Text;
        //中文文摘
        kjcgPM.zwwz = this.zwwzTextBox.Text;
        kjcgPM.ktly = this.ktlyTextBox.Text;//课题来源
        kjcgPM.pjfs = this.pjfsTextBox.Text;//评价方式
        kjcgPM.pjdw = this.pjdwTextBox.Text;//评价单位
        if(!"".Equals(this.pjrqTextBox.Text))
            kjcgPM.pjrq = DateTime.Parse(this.pjrqTextBox.Text);//评价日期

        kjcgPM.pjyj = this.pjyjTextBox.Text;//评价意见
        kjcgPM.jxmc = this.jxmcTextBox.Text;//奖项名称
        kjcgPM.jldw = this.jldwTextBox.Text;//奖励单位
        kjcgPM.jlnd = this.jlndTextBox.Text;//奖励年度
        kjcgPM.sjdj = this.sjdjTextBox.Text;//授奖等级
        kjcgPM.cqxs = this.cqxsTextBox.Text;//产权形式
        kjcgPM.cqmc = this.cqmcTextBox.Text;//产权名称
        kjcgPM.cqbs = this.cqbsTextBox.Text;//产权标识
        kjcgPM.dydw = this.dydwTextBox.Text;//第一单位
        kjcgPM.dwsx = this.dwsxTextBox.Text;//单位属性
        kjcgPM.lsgx = this.lsgxTextBox.Text;//隶属关系
        kjcgPM.txdz = this.txdzTextBox.Text;//通讯地址
        kjcgPM.yzbm = this.yzbmTextBox.Text;//邮政编码
        kjcgPM.lxdh = this.lxdhTextBox.Text;//联系电话
        kjcgPM.czdh = this.czdhTextBox.Text;//传真电话
        kjcgPM.lxr = this.lxrTextBox.Text;//联系人
        kjcgPM.dzxx = this.dzxxTextBox.Text;//电子邮箱
        kjcgPM.qtdw = this.qtdwTextBox.Text;//其他单位
        kjcgPM.dywcr = this.dywcrTextBox.Text;//第一完成人
        kjcgPM.xb = this.xbDropDownList.SelectedValue;//性别
        kjcgPM.zwmc = this.zwmcTextBox.Text;//职务名称
        kjcgPM.szdw = this.szdwTextBox.Text;//所在单位
        kjcgPM.dh = this.dhTextBox.Text;//电话
        kjcgPM.qtwcr = this.qtwcrTextBox.Text;//其他完成人
        kjcgPM.tjdwyj = this.tjdwyjTextBox.Text;//推荐单位意见
        kjcgPM.gx = this.gxCheckBox.Checked;//共享
        kjcgPM.gxrq = DateTime.Now;//更新日期
        kjcgPM.dlr = this.dlrTextBox.Text;//登录人

        return kjcgPM;
    }

    protected void submitButton_Click(object sender, EventArgs e)
    {
        //验证日期
        if (!this.ValidateRQ(this.qsrqTextBox.Text,"起始日期")
            ||!this.ValidateRQ(this.zzrqTextBox.Text,"终止日期")
            ||!this.ValidateRQ(this.pjrqTextBox.Text,"评价日期"))
        {
            return;
        }
        //验证中文名称
        if (!this.ValidateZWMC())
        {
            return;
        }
      
        KJCG_PM kjcgPM = this.GetKJCGPM();
        if (this.kjcgDAO.UpdateKJCG_PM(kjcgPM, ((User)Session["login"]).name))
        {
            Response.Write("<Script>alert('更新科技成果信息成功')</Script>");
            this.NextPM();
        }
        else
        {
            Response.Write("<Script>alert('更新科技成果信息失败')</Script>");
        }
    }

    /// <summary>
    /// 验证中文名称
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateZWMC()
    {
        Boolean flag = true;

        this.errorLabel.Text = "";
        if(null==this.zwmcTextBox.Text||"".Equals(this.zwmcTextBox.Text.Trim()))
        {
            this.errorLabel.Text = "错误：中文名称不能为空";
            flag = false;
        }
        return flag;
    }

    /// <summary>验证日期</summary>
    private Boolean ValidateRQ(String rq, String msg)
    {
        this.errorLabel.Text = "";

        Boolean flag = true;
        if (rq != "")
        {
            try
            {
                DateTime val = DateTime.Parse(rq);
            }
            catch (Exception)
            {
                this.errorLabel.Text = "错误：" + msg + "格式不正确(yyyy-MM-dd).";
                return false;
            }
        }
        return flag;
    }
   
    //全角转半角
    protected void resetButton_Click(object sender, EventArgs e)
    {
        this.BindData();
    }
    protected void gtflTextBox_TextChanged(object sender, EventArgs e)
    {
        this.gtflTextBox.Text = GlobalMethod.Semicolon(this.gtflTextBox.Text.Trim());
    }
    protected void zwztcTextBox_TextChanged(object sender, EventArgs e)
    {
        this.zwztcTextBox.Text = GlobalMethod.Semicolon(this.zwztcTextBox.Text.Trim());
    }
    protected void dydwTextBox_TextChanged(object sender, EventArgs e)
    {
        this.dydwTextBox.Text = GlobalMethod.Semicolon(this.dydwTextBox.Text.Trim());
    }
    protected void qtdwTextBox_TextChanged(object sender, EventArgs e)
    {
        this.qtdwTextBox.Text = GlobalMethod.Semicolon(this.qtdwTextBox.Text.Trim());
    }
    protected void qtwcrTextBox_TextChanged(object sender, EventArgs e)
    {
        this.qtwcrTextBox.Text = GlobalMethod.Semicolon(this.qtwcrTextBox.Text.Trim());
    }

    /// <summary>
    /// 设置前一篇和后一篇的链接
    /// </summary>
    private void SetPreAndNextPM()
    {
        //上一篇
        String pre = this.GetDirectionPM(-1);
        if (null != pre)
        {
            this.preHyperLink.NavigateUrl = GlobalKJCG.EDITPM_PAGE + "?cgdm=" + pre;
        }

        //下一篇
        String next = this.GetDirectionPM(1);
        if (null != next)
        {
            this.nextHyperLink.NavigateUrl = GlobalKJCG.EDITPM_PAGE + "?cgdm=" + next;
        }
    }
    /// <summary>
    /// 修改下一篇
    /// </summary>
    private void NextPM()
    {
        String cgdm = this.GetDirectionPM(1);
        if (null == cgdm)
        {
            return;
        }
        else
        {
            Response.Redirect(GlobalKJCG.EDITPM_PAGE + "?cgdm=" + cgdm);
        }
    }

    /// <summary>
    /// 获取下一个流水号
    /// </summary>
    /// <param name="cgdm"></param>
    /// <param name="direction">大于0下一篇；反之，上一篇</param>
    /// <returns></returns>
    private String GetDirectionPM(int direction)
    {
        String result = null;
        //获取后三四流水号，并在其基础上加1
        String phString = this.cgdmTextBox.Text.Split('-')[1];

        int ph = 0;

        try
        {
            ph = Int32.Parse(phString);
        }
        catch (Exception)
        {
        }

        //当前篇号+1或-1
        if (direction > 0)
        {
            ph++;
        }
        else
        {
            ph--;
        }

        phString = ph.ToString();
        //长度为1
        if (phString.Length == 1)
        {
            phString = "000" + phString;
        }
        else if (phString.Length == 2)
        {
            phString = "00" + phString;
        }
        else if (phString.Length == 3)
        {
            phString = "0" + phString;
        }

        result = this.cbnfTextBox.Text + "-" + phString;
        KJCG_PM KJCGPM = this.kjcgDAO.GetKJCG_PMByCgdm(result);
        if (null == KJCGPM)
        {
            return null;
        }
        return result;
    }
}